package com.baosight.sczx.zm.cm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.OrgEmp;
import com.baosight.sczx.zm.cm.domain.ZMCMDepotpeopleInfo;

/**材料使用--库房信息从表权限管理
 * <p>Title: ServiceZMCM07.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 余虎
 * @version 1.0
 * <pre>Histroy:
 *          2011-5-27 下午06:50:31  余虎  Create
 *</pre>
 */
public class ServiceZMCM12 extends ServiceEPBase {


	/**数据初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30上午10:46:00 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(this.getEmpName());
		return outInfo;
	}
	
	/**数据查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27下午06:51:51 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdDepotId = inInfo.getString("fdDepotId");
		StaticTools.setQueryParameters(inInfo, "fdDepotId", fdDepotId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCMDepotpeopleInfo.query", new ZMCMDepotpeopleInfo());
		return outInfo;
	}
	
	/**新增--
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27  下午06:51:51 余虎
	 *    2011-6-7   下午12:25:51 余虎---人员不确保有重名所以不进行重复校验
	 *    2011-09-13 下午15:38:51 张坤---人员ID确保不会重名
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validate = this.validate(inInfo, "ZMCMDepotpeopleInfo.validate", new HashMap<String, String>());
		if(validate) {
			return inInfo;
		}
		else {
			if(inInfo.getBlock("detailsResult") == null){
				inInfo.addBlock("detailsResult");
			}
			for(int i = 0; i < inInfo.getBlock("detailsResult").getRowCount(); i++){
				String fdDepotId = inInfo.getString("fdDepotId");
				String fdManagmentPermission = inInfo.addBlock("detailsResult").getCellStr(i, "fdManagmentPermission");
				
				if(fdManagmentPermission == null || fdManagmentPermission.equals("")){
					fdManagmentPermission="0"; 
				}
				
				inInfo.getBlock("detailsResult").setCell(i, "fdManagmentPermission", fdManagmentPermission);
				inInfo.getBlock("detailsResult").setCell(i, "fdDepotId", fdDepotId);//主表ID
				String KeyID= TablePk.getInstance().getNextValue("T_CM_DEPOTPEOPLE_INFO");
				inInfo.getBlock("detailsResult").setCell(i, "fdKeyId",KeyID );//主键
			}	
			EiInfo outInfo = super.insert(inInfo, "ZMCMDepotpeopleInfo.insert", "detailsResult");
			return outInfo;
		}
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27下午06:51:51 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		return super.update(inInfo);
	}

	/**删除数据
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-27下午06:51:51 余虎
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCMDepotpeopleInfo.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 构造人员下拉框值
	 * <p>Function: getOrgName</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-31下午06:48:28 余虎
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private EiBlock getEmpName(){
	    EiBlock block = new EiBlock("cmes.cm.empName");
			
	    EiColumn eiColumn = new EiColumn("empCode");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("empName");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<Map> list = OrgEmp.getEmpNames();
		
		block.setRows(list);
		return block;	
	}
	
	/**
	 * 人员重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2下午01:19:23 余虎
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdPeopleId = inInfo.getBlock("detailsResult").getCellStr(0, "fdPeopleId");
		String fdDepotId = inInfo.getString("fdDepotId");
		params.put("fdPeopleId", fdPeopleId);
	    params.put("fdDepotId", fdDepotId);

		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("提交失败，该库房下人员重复 ！");
			return true;
		}
		return false;
	}

}
